<?php

defined('BASEPATH') or die('forbidden');

class Managerpkg_Controller Extends Controller {
	
	private $buglist_db;	//问题列表专用数据库
	private $pkglist;	//对照表
	
    function _init() {
        //check_level(50, segment(0), segment(1));
        $this->buglist_db = $this->load->database('buglist');
        $this->pkglist = array(
        		'android' =>'android',	//android手机
        		'ios' =>'ios',	//ios手机
        		'unknow' =>'unknow',	//不知道啥手机
        );
       
    }
	
    function index() {
        $this->view->display("buglist/managerpkg/index");
    }
    
	function flexigrid() 
	{
		 
		list($colkey, $colsinfo, $where, $sortname, $sortorder, $offset, $rp, $page) = $this->common->get_flexigrid_params();
		 
	
		$sql = "SELECT COUNT(*) AS num FROM pkgdownload	 WHERE {$where} ";
		//echo " sql2:$sql ";
		$one = $this->buglist_db->one($sql);
		$total = $one['num'];
		$orderby = $sortname ? "ORDER BY {$sortname} {$sortorder} " : "";
		 
		$sql = "SELECT {$colsinfo} FROM pkgdownload  WHERE {$where}  {$orderby} LIMIT {$offset},{$rp}";
		$this->buglist_db->query($sql);
		//$id = 0;
		while ($n = $this->buglist_db->get_array('default'))
		{
			$n['inserttime'] = date('Y-m-d H:i:s',$n['inserttime']);
			
			$rows[] = array('id' => $n['id'], 'cell' => $n);
			
		}
		//var_dump($rows);
		$result = array(
	            'page' => $page,
	            'total' => $total,
	            'rows' => $rows
		);
		 
		echo json_encode($result);
	}

    function add()
    {     
       if (is_submit()) {
            $rowdata = $_POST['row'];
            
            //先查下是否有此包
            $sql = " select * from pkgdownload where pkgname='".$rowdata['pkgname']."'";
            $row = $this->buglist_db->one($sql);
            if(count($row)>0)
            {
            	cp_tips('error');
            }
            
            //把时间加上去
            $rowdata['inserttime'] = time();
            //print_r($rowdata);
            $result = $this->buglist_db->insert('pkgdownload',$rowdata);
            
            cp_tips($result > 0 ? "success" : "error");
        }
		$this->view->assign('imglist',$this->pkglist);
    	$this->view->display("buglist/managerpkg/add");
    }
    
	function del($id = 0)
    {    	
     	if ($id === 0)
    	 {
            error(translate('C_INVALID_PARAM_ID', $id));
    	 }         
        
		$c = 0;
        $r = '';
 
        if ($this->buglist_db->delete('pkgdownload',array('id' => $id))) {
            insert_log(translate('MENU_BUGLIST_MANAGERPKG'),translate('DEL'), array('id'=>$id));
            $c = 1;
            $r = translate('C_TIPS_SUCCESS_TEXT');
        } else {
            $r = translate('C_TIPS_ERROR_TEXT');
        }
        json_output($c, $r);
    }
	
    function show()
    {
    	require_once 'MobileDetect.php';
    	
    	//先从数据库中读取到设备的配置来
    	$sql = "SELECT * FROM pkgdownload  order by pkgweight desc";
    	$this->buglist_db->query($sql);
    	//$id = 0;
    	//下载地址列表
    	$downurl = array();
    	
    	while ($n = $this->buglist_db->get_array('default'))
    	{
    		$downurl[$n['pkgname']] = $n['pkgurl'];    			
    	}
    	
    	//print_r($downurl);
    	
    	$mob = new MobileDetect();

    	if($mob->isAndroidOS())
    	{
    		//echo ' android ';
    		$url = "Location:".$downurl['android'];
    		echo " url = ".$url;
    		header($url);
    	}
    	else if($mob->isAndroidOS() )
    	{
    		//echo " ios ";
    		$url = "Location:".$downurl['ios'];
    		echo " url = ".$url;
    		header($url);
    	}
    	else
    	{
    		$url = "Location:".$downurl['unknow'];
    		echo " url = ".$url;
    		header($url);
    	}
    	
    }
    
	function edit($id)
    {     
    	if (!$id)
		{
			error(translate('C_INVALID_PARAM_ID', $id));
		}
		$row = $this->db->one("SELECT * FROM pkgdownload WHERE id={$id}");
		if (!is_array($row))
		{
			error(translate('C_INVALID_PARAM_ID', $id));
		}
		
       if (is_submit()) {
            $rowdata = $_POST['row'];
            
            $rowdata['inserttime'] = time();
            
            $result = $this->buglist_db->update('pkgdownload',$rowdata,array("id"=>$id));
            
            cp_tips($result > 0 ? "success" : "error");
        }
        
        //$rowdata['inserttime'] = date('Y-m-d H:i:s', $rowdata['starttime']);
        $this->view->assign('imglist',$this->pkglist);
		$this->view->assign('row',$row);
    	$this->view->display("buglist/managerpkg/edit");
    }
}